package com.qsbc.pygram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbc.pygram.common.Constants;
import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.Checkpoint;
import com.qsbc.pygram.mapper.CheckpointMapper;
import com.qsbc.pygram.service.CheckpointService;
import com.qsbc.pygram.service.impl.CheckpointServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.management.LockInfo;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-10-29
 */
@RestController
@RequestMapping("/pygram/checkpoint")
public class CheckpointController {
    @Autowired
    private CheckpointService checkpointService;

//    查所有
    @GetMapping
    public List<Checkpoint> findlist() {
        return checkpointService.list(null);
    }

//    增加
    @PostMapping
    public Result save(@RequestBody Checkpoint checkpoint) {
        Integer exist = checkpointService.findByid(checkpoint);

        if (checkpoint.getCheckpointId() == null && checkpoint.getId()==exist) {
            return Result.error();
        }
        return Result.success(checkpointService.saveOrUpdate(checkpoint));

    }


    //    根据id删除
    @DeleteMapping("{checkpointId}")
    public Result delete(@PathVariable String checkpointId) {
        return Result.success(checkpointService.removeById(checkpointId));
    }


    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<String> checkpointIds) {
        return Result.success(checkpointService.removeByIds(checkpointIds));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") Integer id,
                           @RequestParam(defaultValue = "") String checkpointTitle) {

        return Result.success(checkpointService.findPage(new Page<>(pageNum, pageSize), id,checkpointTitle));
    }

}




